package com.focusai.efairy.config;

import android.content.Context;
import android.os.Environment;

import com.focusai.efairy.EFApplication;
import com.focusai.efairy.utils.StorageUtils;

import java.io.File;

/**
 * @author luo_gw
 * Created by kingdee on 2016/4/29.
 */
public class AppDirFileUtil {
    /** 默认APP根目录. */
    private static String RootDirPath = null;

    /** 日志目录路径*/
    public static String LogDirPath = null;

    /** glide cache目录路径*/
    public static String GlideCacheDirPath = null;

    /** glide cache目录路径*/
    public static String ImagePath = null;


    /**
     * 描述：初始化存储目录.
     *
     */
    public static void initFileDir(){

        Context context = EFApplication.getContext();
        //默认下载文件根目录.
        String rootPath = File.separator + AppConfig.BASE_PATH_NAME + File.separator;

        //日志目录
        String logDirPath = rootPath + AppConfig.PATH_LOG_NAME + File.separator;

        //glide cache 目录
        String glideCacheDirPath = rootPath + AppConfig.GLIDE_CACHE_IMAGE_NAME + File.separator;

        //image 目录
        String imagePath = rootPath + AppConfig.PATH_IMAGE_NAME + File.separator;

        File root = null;
        try {
            if(!isCanUseSD()){
                // 把图片缓存到应用data/data/包/...下
                root =context.getCacheDir();
            }else{
                root = Environment.getExternalStorageDirectory();
            }
            if (root == null)
                return;
            File downloadDir = new File(root.getAbsolutePath() + rootPath);
            if(!downloadDir.exists()){
                downloadDir.mkdirs();
            }
            RootDirPath = downloadDir.getPath();

            File fileMineHeadDirFile = new File(root.getAbsolutePath() + logDirPath);
            if(!fileMineHeadDirFile.exists()){
                fileMineHeadDirFile.mkdirs();
            }
            LogDirPath = fileMineHeadDirFile.getPath();

            File glideCacheFile = new File(root.getAbsolutePath() + glideCacheDirPath);
            if(!glideCacheFile.exists()){
                glideCacheFile.mkdirs();
            }
            GlideCacheDirPath = glideCacheFile.getPath();

            File ImagePatheFile = new File(root.getAbsolutePath() + imagePath);
            if(!ImagePatheFile.exists()){
                ImagePatheFile.mkdirs();
            }
            ImagePath = ImagePatheFile.getPath();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getRootDirPath() {
        if (RootDirPath == null){
            initFileDir();
        }
        return RootDirPath;
    }


    public static String getGlideCacheDirPath() {
        if (GlideCacheDirPath == null){
            initFileDir();
        }
        return GlideCacheDirPath;
    }

    public static String getLogDirPath() {
        if (LogDirPath == null) {
            initFileDir();
        }
        return LogDirPath;
    }

    public static String getImageDirPath() {
        if (ImagePath == null) {
            initFileDir();
        }
        return ImagePath;
    }

    /**
     * 描述：SD卡是否能用.
     *
     * @return true 可用,false不可用
     */
    public static boolean isCanUseSD() {
        try {
            return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    public static File getLocalFile(Context context, String path) {
        File appCacheDir = new File(StorageUtils.getCacheDirectory(context), path);
        if (!appCacheDir.exists()) {
            createLocalFile(context, path);
        }
        return appCacheDir;
    }
    public static void createLocalFile(Context context, String path) {
        File appCacheDir = new File(StorageUtils.getCacheDirectory(context), path);
        if (!appCacheDir.exists()) {
            appCacheDir.mkdirs();
        }
    }
}
